package com.lovo.ssc.controller;


import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lovo.ssc.bean.Commodity;
import com.lovo.ssc.bean.Commodityimg;
import com.lovo.ssc.bean.OrderGoods;
import com.lovo.ssc.dto.OrderGoodsDto;
import com.lovo.ssc.dto.WebCommodityDto;
import com.lovo.ssc.service.ICommodityService;
import com.lovo.ssc.service.ICommodityimgService;
import com.lovo.ssc.service.IOrderGoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 订单商品中间表 前端控制器
 * </p>
 *
 * @author lovo
 * @since 2022-07-12
 */
@RestController
@RequestMapping("ordergoods")
@Api(tags = "购买订单明细")
public class OrderGoodsController {

    @Autowired
    IOrderGoodsService orderGoodsService;
    @Autowired
    ICommodityService commodityService;

    @Autowired
    ICommodityimgService commodityimgService;


    @GetMapping("findByOrderCode/{orderCode}")
    @ApiOperation(value = "查看购买订单明细")
    public List<OrderGoodsDto> findByOrderCode(@PathVariable("orderCode") String orderCode){
        List<OrderGoodsDto> list=new ArrayList<>();
        QueryWrapper<OrderGoods> qw=new QueryWrapper<>();
        qw.eq("orderCode",orderCode);
        List<OrderGoods> orderGoods=orderGoodsService.list(qw);
        for (OrderGoods o:orderGoods){
            QueryWrapper<Commodity> qw2=new QueryWrapper<>();
            qw2.eq("commodityCode",o.getGoodsCode());
            qw2.eq("commodityBatch",o.getGoodsBatch());
            Commodity com=commodityService.getOne(qw2);

            System.out.println(com);
            QueryWrapper<Commodityimg> qwi= new QueryWrapper<>();
            qwi.eq("fkCommodityCode",com.getCommoditycode());

            List<Commodityimg> img=commodityimgService.list();

            OrderGoodsDto cdto=new OrderGoodsDto();
            cdto.setCommoditycode(com.getCommoditycode());
            cdto.setCommodityname(com.getCommodityname());
            cdto.setType(com.getType());
            cdto.setPrice(Float.valueOf(String.valueOf(com.getPrice())));
            cdto.setNumber(o.getGoodNumber());
            for (Commodityimg im:img){
                cdto.setImgpathList(im.getImgpath());
            }

            list.add(cdto);

        }

        return list;
    }
    @GetMapping("test")
    @ApiOperation(value = "test")
    public Commodity test(String code){
        QueryWrapper<Commodity> qw2=new QueryWrapper<>();
        qw2.eq("commodityCode",code);
        Commodity com=commodityService.getOne(qw2);

        return com;
    }


}
